<template>
  <div>
    <el-card class="page-container">
      <template #header>
        <div class="card-header">
          <span>报销汇款管理</span>
        </div>
      </template>
      
      <!-- 搜索表单 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="身份证号">
          <el-input v-model="searchForm.card_id" placeholder="请输入身份证号" clearable />
        </el-form-item>
        <el-form-item label="参合人姓名">
          <el-input v-model="searchForm.insured_name" placeholder="请输入姓名" clearable />
        </el-form-item>
        <el-form-item label="汇款状态">
          <el-select v-model="searchForm.remit_status" placeholder="请选择" clearable>
            <el-option label="待汇款" :value="0" />
            <el-option label="已汇款" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 数据表格 -->
      <el-table :data="tableData" border style="width: 100%" v-loading="loading">
        <el-table-column prop="card_id" label="身份证号" width="180" />
        <el-table-column prop="insured_name" label="参合人姓名" width="120" />
        <el-table-column prop="rural_card_no" label="参合证号" width="120" />
        <el-table-column prop="disease_name" label="疾病名称" width="150" />
        <el-table-column prop="total_amount" label="医疗总费用" width="120">
          <template #default="scope">
            {{ scope.row.total_amount.toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column prop="apply_amount" label="报销金额" width="120">
          <template #default="scope">
            {{ scope.row.apply_amount.toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column prop="apply_time" label="申请时间" width="180" />
        <el-table-column prop="remit_status" label="汇款状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.remit_status === 0 ? 'warning' : 'success'">
              {{ scope.row.remit_status === 0 ? '待汇款' : '已汇款' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="scope">
            <el-button 
              v-if="scope.row.remit_status === 0"
              type="primary" 
              size="small" 
              @click="handleDetail(scope.row)"
            >汇款</el-button>
            <el-button 
              v-else
              type="info" 
              size="small" 
              @click="handleDetail(scope.row)"
            >详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>

      <!-- 详情对话框 -->
      <el-dialog
        v-model="detailDialogVisible"
        :title="currentDetail.remit_status === 0 ? '报销汇款' : '汇款详情'"
        width="50%"
      >
        <el-descriptions :column="2" border>
          <el-descriptions-item label="身份证号">{{ currentDetail.card_id }}</el-descriptions-item>
          <el-descriptions-item label="参合人姓名">{{ currentDetail.insured_name }}</el-descriptions-item>
          <el-descriptions-item label="参合证号">{{ currentDetail.rural_card_no }}</el-descriptions-item>
          <el-descriptions-item label="疾病编码">{{ currentDetail.disease_code }}</el-descriptions-item>
          <el-descriptions-item label="疾病名称">{{ currentDetail.disease_name }}</el-descriptions-item>
          <el-descriptions-item label="医疗总费用">{{ currentDetail.total_amount?.toFixed(2) }}</el-descriptions-item>
          <el-descriptions-item label="报销金额">{{ currentDetail.apply_amount?.toFixed(2) }}</el-descriptions-item>
          <el-descriptions-item label="申请时间">{{ currentDetail.apply_time }}</el-descriptions-item>
          <el-descriptions-item label="报销年度">{{ currentDetail.apply_year }}</el-descriptions-item>
          <el-descriptions-item label="操作员">{{ currentDetail.operator_name }}</el-descriptions-item>
          <el-descriptions-item label="汇款状态">
            <el-tag :type="currentDetail.remit_status === 0 ? 'warning' : 'success'">
              {{ currentDetail.remit_status === 0 ? '待汇款' : '已汇款' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="行政区域编码">{{ currentDetail.area_code }}</el-descriptions-item>
          <template v-if="currentDetail.remit_status === 1">
            <el-descriptions-item label="汇款时间">{{ currentDetail.remit_time }}</el-descriptions-item>
            <el-descriptions-item label="汇款操作员">{{ currentDetail.remit_operator }}</el-descriptions-item>
          </template>
        </el-descriptions>
        
        <!-- 汇款操作按钮 -->
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="detailDialogVisible = false">关闭</el-button>
            <template v-if="currentDetail.remit_status === 0">
              <el-button type="primary" @click="handleRemit(currentDetail)">
                确认汇款
              </el-button>
            </template>
          </span>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 搜索表单数据
const searchForm = reactive({
  card_id: '',
  insured_name: '',
  remit_status: ''
})

// 表格数据
const tableData = ref([])
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 详情对话框
const detailDialogVisible = ref(false)
const currentDetail = ref({})

// 模拟数据
const mockData = [
  {
    id: 1,
    card_id: '330124199001015566',
    insured_name: '张三',
    rural_card_no: 'RC20240001',
    disease_code: 'E11.902',
    disease_name: '2型糖尿病',
    total_amount: 5680.50,
    apply_amount: 3408.30,
    apply_time: '2024-03-15 09:30:00',
    apply_year: '2024',
    operator_id: 1001,
    operator_name: '李四',
    remit_status: 0,
    area_code: '330124'
  },
  {
    id: 2,
    card_id: '330124196505204433',
    insured_name: '王五',
    rural_card_no: 'RC20240002',
    disease_code: 'I10.x01',
    disease_name: '原发性高血压',
    total_amount: 3280.00,
    apply_amount: 1968.00,
    apply_time: '2024-03-16 14:20:00',
    apply_year: '2024',
    operator_id: 1001,
    operator_name: '李四',
    remit_status: 1,
    remit_time: '2024-03-17 10:30:00',
    remit_operator: '赵六',
    area_code: '330124'
  }
]

// 查询数据
const handleSearch = async () => {
  loading.value = true
  try {
    // 使用模拟数据
    setTimeout(() => {
      const filteredData = mockData.filter(item => {
        const cardIdMatch = !searchForm.card_id || item.card_id.includes(searchForm.card_id)
        const nameMatch = !searchForm.insured_name || item.insured_name.includes(searchForm.insured_name)
        const statusMatch = searchForm.remit_status === '' || item.remit_status === searchForm.remit_status
        return cardIdMatch && nameMatch && statusMatch
      })
      tableData.value = filteredData
      total.value = filteredData.length
      loading.value = false
    }, 500)
  } catch (error) {
    ElMessage.error('获取数据失败')
    loading.value = false
  }
}

// 重置搜索
const resetSearch = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = ''
  })
  handleSearch()
}

// 查看详情
const handleDetail = (row) => {
  currentDetail.value = { ...row }
  detailDialogVisible.value = true
}

// 处理汇款
const handleRemit = async (row) => {
  try {
    await ElMessageBox.confirm(
      `确认要对${row.insured_name}的报销申请进行汇款操作吗？`,
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    // 直接修改模拟数据
    const index = mockData.findIndex(item => item.id === row.id)
    if (index !== -1) {
      mockData[index].remit_status = 1
      mockData[index].remit_time = new Date().toLocaleString()
      mockData[index].remit_operator = '赵六'
      detailDialogVisible.value = false
      ElMessage.success('汇款操作成功')
      handleSearch()
    }
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('操作失败')
    }
  }
}

// 分页相关方法
const handleSizeChange = (val) => {
  pageSize.value = val
  handleSearch()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  handleSearch()
}

// 初始化加载数据
handleSearch()
</script>

<style scoped>
.page-container {
  margin: 20px;
  min-height: calc(100vh - 140px);
}
.card-header {
  font-size: 18px;
  font-weight: bold;
}
.search-form {
  margin-bottom: 20px;
}
.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>